package com.thirdsdk.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.curry.android.util.JSONHelper;
import com.common.jump.CommonJumper;
import com.curry.android.util.StringUtil;
import com.datatrack.DataTrack;
import com.orangelife.mobile.main.biz.NetworkImageHolderView;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * 极光推送的自定义接收器
 * <p>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 * <p>
 * create by yw on unknown,modify by wgf on 160714.
 *
 * @author yw
 * @since v3.3.0
 */
public class JpushReceiver extends BroadcastReceiver {

    /**
     * Activit 名称
     **/
    final String MODULE_NAME = "/pushPop";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            return;
        }
        /*通知的标题*/
        String title;
        /*通知的内容*/
        String content;
        /*服务器推送下来的JSON字符串*/
        String extras;
        if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            Map<String, Object> map = JSONHelper.jsonToMap(extras);
            if (map == null) {
                return;
            }
        }
        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {  /*自定义消息接收*/
            /*自定义的推送内容*/
            content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            /**添加自定义键值字段*/
            extras = bundle.getString(JPushInterface.EXTRA_EXTRA);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { /*用户点击打开了通知*/
            title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            //String content = bundle.getString(JPushInterface.EXTRA_ALERT);
            extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            //String type = bundle.getString(JPushInterface.EXTRA_CONTENT_TYPE);
            Map<String, Object> map = JSONHelper.jsonToMap(extras);
            if (map == null) {
                return;
            }
            /* url标准格式： "orangelife://www.17orange.com?mode=goodsDetail&id=d25b25e0-be2e-4f07-a73c-ce092a8c5144";*/
            String webViewUrl = String.valueOf(map.get("uri"));
            //String adverType = String.valueOf(map.get("advertType"));/*此参数已废除*/
            if (!StringUtil.isBlank(webViewUrl)) {
                Uri uri = Uri.parse(webViewUrl);
                if (!uri.isRelative() && !StringUtil.isBlank(title)) {
                    webViewUrl = NetworkImageHolderView.getURL(webViewUrl, title);
                }
            }
            /*所有的JPush来的跳转WebView的动作，都要先跳转到Main界面，再转到WebView界面*/
            CommonJumper.jumpToMainWithNewTask(context, webViewUrl);
        }

    }

    /**
     * 数据采集体不要删除
     *
     * @return
     */
    public Map<String, Object> getDataTrack() {
        Map<String, Object> map = new HashMap<>();
        map.put("UI_HOST", DataTrack.APP_NAME);
        map.put("UI_PATH", MODULE_NAME);
        return map;
    }
}

